Skip to content

Implement function autoloading (mark 5) - #22337

Open
pmjones wants to merge 1 commit into
php:masterfrom
pmjones:function-autoloading
Open

Implement function autoloading (mark 5)#22337
pmjones wants to merge 1 commit into
php:masterfrom
pmjones:function-autoloading

Conversation

@pmjones

@pmjones pmjones commented Jun 16, 2026

Copy link
Copy Markdown

@pmjones
pmjones force-pushed the function-autoloading branch from da508ed to 2904317 Compare July 7, 2026 14:10
@pmjones
pmjones force-pushed the function-autoloading branch 2 times, most recently from 1fe448a to 99e4623 Compare July 15, 2026 01:04
@britkig

britkig commented Aug 3, 2026

Copy link
Copy Markdown

I can understand the reluctance of the others to implement this and prior RFCs on the grounds of performance since it's hardly the case there would be more classes than functions being loaded, but it can be argued that the performance penalties in this regard can be mitigated by implementing namespace-level loading within the coder's callback itself:

function fn_autoload(string $fn):void{

		//	Base path for finding modules
	$path=__DIR__.\DIRECTORY_SEPARATOR.'modules';

		//	Trim, normalize and build base file path string
	$fn = $path.\DIRECTORY_SEPARATOR.\strtolower(\ltrim($fn,'\\/'));

		 //Use namespace-based or function-based loading?		 
	$nsmode=true;
	$fn= ($nsmode ? \dirname($fn) : $fn). '.func.php';

	//	Include the file! (contains both `ExistsDir` and `ExistFile`)
	@include_once $fn;

}

//	
//	Place callback-registering function call here
//	

	//	Examples
\IO\ExistsFile('/path/to/file.txt');	//	Autoload triggers
\IO\ExistsDir('/path/to/directory');	//	No autoload trigger

In this example, when mentioning \IO\ExistsFile, the auto-loader triggers, because it hasn't been declared. So far, so good. Then, if ExistsDir exists within the same file as ExistsFile, this should cause the PHP engine to skip running the autoloader for a second time outright when it is encountered, because it has already been declared before during the same loop when ExistFile was originally being searched for.

This approach would not eliminate all of the performance penalties, but would create an opening to give the coder the option to manipulate within the callback to load beyond the scope of a single function, because it's all about the timing of what is encountered first.

Natively locking the engine itself to namespace-based loading may be too restrictive; in some usage scenarios, loss of performance for function-level auto-loading may be a necessary or negligible trade-off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants